<template>
  <div class="warp">
    <div class="warp_auto">
      <div class="warp_top">
        <div class="jiansuo-box">
          <span class="label">床位名称：</span>
          <el-input
            v-model="keshiname"
            class="input"
            placeholder="请输入床位名称"
            clearable
          />
        </div>

        <div class="jiansuo-box">
          <span class="label">床位状态：</span>
          <el-select
            v-model="value"
            placeholder="请选择状态"
            clearable
            class="input"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <el-button
          type="primary"
          icon="el-icon-search"
          class="btn_"
          @click="btn"
          >检索</el-button
        >
      </div>

      <el-card>
        <el-table
          :data="arr"
          style="width: 100%"
          stripe
          :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
        >
          <el-table-column type="index"label="序号" align="center" />
          <el-table-column label="床位编码" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.bed_code }}</span>
            </template>
          </el-table-column>
          <el-table-column label="床位名称" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.bed_name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            property="status"
            label="状态"
            align="center"
            header-align="center"
          >
            <template scope="scope">
              <el-switch
                v-model="scope.row.status"
                class="switch-btn"
                inactive-text="禁用"
                active-text="启用"
                :active-value="1"
                :inactive-value="0"
                active-color="#02538C"
                inactive-color="#B9B9B9"
                @change="changeSwitchs(scope.$index, scope.row)"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button disabled size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
              >

              <el-popover
                :ref="`popover-${scope.$index}`"
                trigger="click"
                placement="top"
                width="100"
              >
                <p class="el-icon-warning">确定删除</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="no(scope)"
                  >取消</el-button
                  >
                  <el-button type="primary" size="mini" @click="yes(scope)"
                  >确定</el-button
                  >
                </div>
                <el-button
                  slot="reference"
                  size="mini"
                  style="margin-left: 10px"
                  type="danger"
                  disabled
                >删除</el-button
                >
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        <component-dialog
          :dialog-visible="dialogVisible"
          :dialog-info="dialogInfo"
          @update:dialogVisible="dialogVisibles"
        />
        <div class="paging">
          <el-pagination
            :current-page="start"
            :page-size="PageSize"
            :pager-count="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
    <!-- switch -->
    <el-dialog title="状态更新" :visible.sync="dialogUpdata" class="mod_box1">
      <span>
        <i class="el-icon-warning" />
        此操作将会改变开关状态, 是否继续?</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="(dialogUpdata = false), cancelSwitch()"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="(dialogUpdata = false), updataSwitch()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import componentDialog from "@/components/mask/index";
import { List, add, check, edit, Delete, update } from "@/api/table";
import { MessageBox, Message } from "element-ui";

export default {
  components: {
    componentDialog,
  },
  data() {
    return {
      dialogUpdata: false, // 开关
      // 点击查看按钮  这条数据详细信息
      dialogInfo: {},
      listarr: null,
      listLoading: true,
      loading: false,
      keshiname: "",
      dialogFormVisible: false,
      del: false,
      ruleForm: {
        bedCode: "",
        bedName: "",
        status: "",
      },
      options: [
        {
          id: "0",
          value: "1",
          label: "启用",
        },
        {
          id: "1",
          value: "0",
          label: "禁用",
        },
      ],
      value: "",
      arr: [],
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意：这里不能为空)
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10, // 默认每页显示的条数（可修改）
      dialogVisible: false, // 控制弹窗 显示
      // 点击查看按钮  这条数据详细信息
      switchId: "",
      switchStatus: "",
    };
  },
  created() {
    this.lastTime = new Date().getTime();
  },
  mounted() {
    this.list(this.PageSize, this.start);
  },
  methods: {
    yes(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      console.log(scope.row.id);
      const id = scope.row.id;
      Delete({ id: id }).then((res) => {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.list(this.PageSize, this.start);
      });
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },
    no(scope) {
      this.$message("取消删除");
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },
    // 新增
    submitForm() {
      add(this.ruleForm).then((res) => {
        this.arr = res.data;
        this.list(this.PageSize, this.start);
      });
    },
    // 列表开关的id
    changeSwitchs(index, row) {
      this.switchId = row.id;
      this.switchStatus = row.status;
      this.dialogUpdata = true;
    },
    // 保存开关状态-确认
    updataSwitch() {
      update({ id: this.switchId, status: this.switchStatus }).then((res) => {
        this.list(this.PageSize, this.start);
      });
    },
    // 保存开关状态-取消
    cancelSwitch() {
      this.list(this.PageSize, this.start);
    },
    // 检索按钮
    btn() {
      this.start = 1;
      List({
        bedName: this.keshiname,
        status: this.value === "" ? null : Number(this.value),
        size: this.PageSize,
        start: this.start,
      }).then((res) => {
        this.arr = res.data;
        this.totalCount = res.totalCount;
      });
    },
    handleEdit(info) {
      // 弹窗打开
      this.dialogVisible = true;
      this.dialogInfo = info;
      console.log(info);
    },
    // 子组件传 过来的 数据
    dialogVisibles(v) {
      this.dialogVisible = v;
      console.log(v);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    list(n1, n2) {
      List({
        size: n1,
        start: n2,
        bedName: this.keshiname,
        status: this.value === "" ? null : Number(this.value),
      }).then((res) => {
        if (res.code === 200) {
          this.arr = res.data;
          this.totalCount = res.totalCount;
        } else if (res.code == 3) {
          Message({
            message: res.msg,
            type: "error",
            duration: 5 * 1000,
          });
          this.$router.push(`/login?redirect=${this.$route.fullPath}`);
        }
      });
    },
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 点击每页显示的条数时，显示第一页
      this.list(val, 1);
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.start = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.start = val;

      // 切换页码时，要获取每页显示的条数
      this.list(this.PageSize, (this.start = val));
    },
    change: function (index, row) {
      console.log(index, row, "---");
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
